package com.exam.service.privilege.impl;

import javax.persistence.Query;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.exam.service.base.DaoSupport;
import com.exam.service.privilege.DepartmentService;

@Service
@Transactional
public class DepartmentServiceBean extends DaoSupport implements DepartmentService {

	@Transactional(readOnly=true, propagation=Propagation.NOT_SUPPORTED)
	public boolean exist(String name) {	
		
		return ((Long)em.createQuery("select count(o) from Department o where o.name=?1")
		.setParameter(1, name).getSingleResult())>0;
	}
	
	public void setVisible(Integer[] entityIds,Boolean status){
		
		if(entityIds!=null&&entityIds.length>0){
			StringBuffer jpql=new StringBuffer("");
			int i=0;
			for(;i<entityIds.length-1;i++){
				jpql.append("?").append(i+2).append(",");
			}
			jpql.append("?").append(i+2);
			Query query=em.createQuery("update Department o set o.visible=?1 where o.id in("+jpql.toString()+")")
			.setParameter(1, status);
			for(i=0;i<entityIds.length;i++){
				query.setParameter(i+2, entityIds[i]);
			}
			query.executeUpdate();
		}
	}
}
